<template>
  <div class="page" v-loading="loading">
    <div style="display: flex;align-items: center;height: 47px;border-bottom: 1px solid #ddd;text-align: center;">
      <div style="flex: 3;border-right: 1px solid #ddd;height: 47px;line-height: 47px;">关卡信息</div>
      <div style="flex: 7;">题目信息</div>
    </div>
    <div style="display: flex;min-height: calc(100vh - 105px - 47px);">
      <div style="flex: 3;border-right: 1px solid #ddd;">
        <div style="margin: 15px;">
          <div style="display: flex;align-items: center;flex-wrap: wrap;">
            <el-button style="width: calc(22vmax / 5);height: calc(22vmax / 5);margin: 0 10px 15px 0px;" type="primary" plain v-for="(item, index) in levelList"
                       @click="getDetailList(item.barrier.id); current.id = item.barrier.id">{{index + 1}}</el-button>
          </div>
          <el-button type="primary" round style="width: 100%;" @click='drawer=true'>新增关卡</el-button>
        </div>
      </div>
      <div style="flex: 7;">
        <div style="margin: 15px;">
          <div style="display: flex;justify-content: flex-end;margin-bottom: 15px;">
            <el-button type="primary" @click="drawer=true; obj = current;">新增题目</el-button>
            <el-button type="danger" @click="batchDelete()">批量删除</el-button>
          </div>
          <div v-if="answerList.length">
            <el-table :data="dataList" border @select="(item) => {deleteSelect = item;}">
              <el-table-column type="selection" width="55"></el-table-column>
              <el-table-column label="唯一编号" prop="id" width="100"></el-table-column>
              <el-table-column label="题目名称" prop="title"></el-table-column>
              <el-table-column label="正确选项" prop="answer"></el-table-column>
              <el-table-column label="展示列表" prop="list"></el-table-column>
            </el-table>
          </div>
          <el-empty v-else description="当前关卡暂无题目信息"></el-empty>
        </div>
      </div>
    </div>
    <el-drawer title="添加题目" :visible.sync="drawer" direction="rtl" :before-close="handleClose" size="40%" >
      <div style="margin-bottom: 15px;">
        <div style="margin-bottom: 15px;display: flex;justify-content: space-between;align-items: center;">
          <div style="margin-left:10px;">已选择的题目</div>
          <div>
            <el-button type="primary" @click="save" v-if="obj == null">添加关卡</el-button>
            <el-button type="primary" @click="save" v-else>新增题目</el-button>
          </div>
        </div>
        <div style="margin-bottom: 15px;">
          <el-table :data="select" border>
            <el-table-column sortable label="唯一编号" width="120" prop="id"></el-table-column>
            <el-table-column label="题目名称" prop="title"></el-table-column>
            <el-table-column label="正确答案" prop="answer"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="danger" size="mini" @click="select = select.filter(item => item.id != scope.row.id)"><i class="el-icon-delete"></i> 删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div style="margin-bottom: 15px; margin-left:10px;">题目列表</div>
        <div style="margin-bottom: 15px;display: flex;align-items: center; margin-left:10px;">
          <el-input v-model="info.name" @keyup.enter.native="initPage" placeholder="请输入题目关键字搜索" style="margin-right: 15px;"></el-input>
          <el-button type="primary" @click="initPage"><i class="el-icon-search"></i> 搜索</el-button>
        </div>
        <el-table :data="answerList" border @select="selectItem">
          <el-table-column type="selection" width="55" ></el-table-column>
          <el-table-column sortable label="编号" width="80" prop="id"></el-table-column>
          <el-table-column label="题目名称" prop="title" width="300"></el-table-column>
          <el-table-column sortable label="正确答案" prop="answer"></el-table-column>
        </el-table>
        <div style="display: flex; justify-content: flex-end; margin-top: 10px;">
          <el-pagination :total="total" :page-size="info.limit" @current-change="currentChange" background layout="prev,pager,next,jumper"></el-pagination>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
  import { id } from "html-webpack-plugin/lib/chunksorter";

  export default {
    name: "manage-level",
    data() {
      return {
        activeName: "",
        drawer: false,
        categoryList: [],
        levelList: [],
        answerList: [],
        total:0,
        info:{
          name:'',
          page:1,
          limit:10,
        },
        dataList:[],
        select:[],
        obj:null,
        current:{
          id:'',
          list:'',
        },
        deleteSelect:[],
        loading:false
      }
    },
    methods: {
      currentChange(page) {
        this.info.page = page;
        this.initPage();
      },
      handleClick() {
        console.log(this.activeName)
      },
      handleClose(done) {
        this.obj = null;
        this.$confirm('当前所选择的题目暂未保存是否确认离开', '提示', {
            type: "warning"
          }).then(_ => {
            done();
          })
          .catch(_ => {});
      },
      initPage() {
        this.$get("/barrier/problem_list", this.info).then(res => {
          if (res.code == 200) {
             this.answerList = res.data.records;
             this.total = res.data.total;
          }
        })
        this.$get("/barrier/list",{changeMethod:0}).then(res => {
          this.levelList = res.data;
          this.current.id = this.levelList[0].barrier.id;
          this.getDetailList(this.levelList[0].barrier.id);
        })
      },
      getDetailList(barrierId){
        this.loading = true
        this.$get("/barrier/detail", {barrierId: barrierId, changeMethod:0}).then(result => {
           this.dataList = result.data;
           this.current.list = this.dataList;
           this.loading = false
        })
      },
      selectItem(item){
         this.select = item;
      },
      save() {
        let ids = this.select.map(item => item.id)
        if(this.obj != null){
           this.current.list.forEach(item =>{
             let flag = false;
             for(let i = 0, length = ids.length; i < length; i++){
               if(item === ids[i]){
                 flag = true; break;
               }
             }
             if(!flag) ids.push(item.id);
           });
        }
        this.$post("/barrier/createOrUpdate", {
          id: this.obj == null ? null : this.obj.id,
          problemIds: ids.toString(),
          changeMethod: 0
        }).then(res => {
          if (res.code == 200) {
            this.drawer = false
            this.initPage()
            this.select = []
            this.searchInfo.content = ''
            this.$message.success("关卡添加成功")
          }
        })
      },
      batchDelete(){
        if(this.deleteSelect.length == 0){
          return this.$message.warning("请选择要删除的题目");
        }
        this.$confirm("是否要删除这些题目？").then(ab => {
            let ids = [];
            this.current.list.forEach(item => {
               let flag = false;
               for(let i = 0, length = this.deleteSelect.length; i < length; i++){
                 if(this.deleteSelect[i].id == item.id){
                    flag = true; break;
                 }
               }
               if(!flag) ids.push(item.id);
            })
          this.$post("/barrier/createOrUpdate", {
            id: this.current.id,
            problemIds: ids.toString(),
            changeMethod: 0
          }).then(res => {
            if (res.code == 200) {
              this.drawer = false
              this.initPage()
              this.select = []
              this.searchInfo.content = ''
              this.$message.success("删除成功");
            }
          })
        })
      }
    },
    created() {
      this.initPage()
    },
    watch: {
      categoryList: {
        deep: true,
        handler(newval, oldval) {
          let data = newval.filter(item => item.flag == true)
          // this.getLevel(data[0].id)
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .page {
    border-radius: 8px;
    background-color: #fff;
    height: calc(100vh - 105px);
    overflow-y: auto;
    box-sizing: border-box;
  }
</style>
